﻿using System;
using System.Collections.Generic;
using System.IO;
using Container.Library;
using Document.Library;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Project.Logic;
using Project.Model;
using Tool.Model;
using Web.Manage.App_Start;

namespace Web.Manage.Areas.Staff.Controllers
{
    /// <summary>
    /// 员工考勤明细操作控制器
    /// </summary>
    [Area("Staff")]
    public class SalaryRecordController : BaseController
    {

        private IHostingEnvironment hostingEnv;
        private readonly IEmployeeService employeeService = null;//员工管理逻辑

        public SalaryRecordController(IHostingEnvironment env)
        {
            this.hostingEnv = env;
            //员工管理逻辑注入
            employeeService = UnityCIContainer.Instance.GetService<IEmployeeService>();
        }

        /// <summary>
        /// 列表界面
        /// </summary>
        /// <returns></returns>
        [UserAuthorization(true, FunctionValidateType.VerifyView)]
        public IActionResult Index()
        {
            return View();
        }



        #region 查询

        /// <summary>
        /// 获取工资单分页列表
        /// </summary>
        /// <returns></returns>
        [UserAuthorization(true, FunctionValidateType.VerifyLoggedIn)]
        public ActionResult LoadEmployeeSalaryRecordList()
        {
            var resultInfo = new ResultJsonInfo<List<T_SalaryCountEntity>>();
            try
            {
                //实体接收数据
                var parametersInfo = DeserializeRequestData<ParametersInfo<SalaryRecordQueryRequest>>();
            
                resultInfo = employeeService.LoadEmployeeSalaryRecordList(parametersInfo);
            }
            catch (Exception ex)
            {
                resultInfo.Success = false;
                resultInfo.Msg = ex.Message;
                resultInfo.Excep = ex;
            }
            return ReturnContent(resultInfo);
        }

        /// <summary>
        /// 导出报表
        /// </summary>
        /// <returns>处理结果</returns>
        [EnableCors("AllowSpecificOrigin")]
        [UserAuthorization(true, FunctionValidateType.VerifyLoggedIn)]
        public ActionResult EpxortSalaryRecord()
        {
            var resultInfo = new ResultJsonInfo<List<T_SalaryCountEntity>>();

            FileStream fs = null;
            BinaryWriter bw = null;

            try
            {
                var parametersInfo = DeserializeRequestData<ParametersInfo<SalaryRecordQueryRequest>>();

                resultInfo = employeeService.LoadSalaryRecordAllList(parametersInfo);

                List<string> headlist = new List<string>()
                {
                   "员工姓名","性别","工种","工资单号","年份","月份","借款金额",
                   "实发工资","考勤天数","加班小时","加班工资","计件工资","身份证号","银行账号",
                   "备注"
                };
                string fileName = "员工工资单信息-" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".xls";

                var savePath = ExcelHelper.ExportToExcelReturnFilePath<T_SalaryCountEntity>(resultInfo.Data, headlist, rowData =>
                {
                    return new object[] {
                                           rowData.Name,
                                           rowData.Sex,
                                           rowData.WorkTypeName,
                                           rowData.FNO,
                                           rowData.MyYear,
                                           rowData.MyMonth,
                                           rowData.LoanMoney,
                                           rowData.RealMoney,
                                           rowData.WorkDay,
                                           rowData.OverTime,
                                           rowData.TOverMoney,
                                           rowData.AmountMoney,
                                           rowData.Code,
                                           rowData.BanKNo,
                                           rowData.Rmark
                           };
                },
                false,
                hostingEnv.WebRootPath,
                fileName
                );
                resultInfo.Msg = savePath;
            }
            catch (Exception ex)
            {
                resultInfo.Success = false;
                resultInfo.Msg = ex.Message;
                resultInfo.Excep = ex;
            }
            finally
            {
                if (fs != null) fs.Close();
                if (bw != null) bw.Close();
            }
            return ReturnContent(resultInfo);
        }

        #endregion

        #region 工资单操作

        /// <summary>
        /// 撤销工资单
        /// </summary>
        /// <returns></returns>
        [UserAuthorization(true, FunctionValidateType.VerifyLoggedIn)]
        public ActionResult CancelSalary()
        {
            var resultInfo = new ResultJsonInfo<int>();
            try
            {
                //实体接收数据
                var parametersInfo = DeserializeRequestData<ParametersInfo<SalaryCancelRequest>>();

                resultInfo = employeeService.CancelSalary(parametersInfo.parameters);
            }
            catch (Exception ex)
            {
                resultInfo.Success = false;
                resultInfo.Msg = ex.Message;
                resultInfo.Excep = ex;
            }
            return ReturnContent(resultInfo);
        }
        #endregion
    }
}